﻿package util
{
	import flash.display.DisplayObject;
	import flash.utils.Dictionary;
	import flash.utils.getTimer;

	import globals.Global;

	/**
	 * 自定义缓动
	 * @author luguobin
	 */
	public class GameTween
	{
		private static var containers:Dictionary = new Dictionary();

		public static function run():void
		{
			var stamp:int = Global.timeStamp;

			for(var key:Object in containers)
			{
				var tween:Tween = containers[key];
				var hadGoTime:int = stamp - tween.startTime;
				hadGoTime -= tween.delay;

				if(hadGoTime <= 0) //还在delay时段
				{
					continue;
				}

				if(tween.duration == 0) //无限时间
				{
					tween.modulus += 0.15;
				}
				else if(hadGoTime >= tween.duration)
				{
					tween.target.x = tween.xTo;
					tween.target.y = tween.yTo;
					tween.target.alpha = tween.alphaTo;
					tween.target.scaleX = tween.scaleXTo;
					tween.target.scaleY = tween.scaleYTo;
					tween.stoped = true;
					tween.target = null;
					delete containers[key];

					if(tween.onComplete != null)
					{
						tween.onComplete.apply(null, tween.onCompleteParams);
					}
					continue;
				}

				switch(tween.type)
				{
					case Tween.ALL:
						tween.target.x = tween.xFrom + (tween.xTo - tween.xFrom) * hadGoTime / tween.duration;
						tween.target.y = tween.yFrom + (tween.yTo - tween.yFrom) * hadGoTime / tween.duration;
						tween.target.alpha = tween.alphaFrom + (tween.alphaTo - tween.alphaFrom) * hadGoTime / tween.duration;
						tween.target.scaleX = tween.scaleX + (tween.scaleXTo - tween.scaleX) * hadGoTime / tween.duration;
						tween.target.scaleY = tween.scaleY + (tween.scaleYTo - tween.scaleY) * hadGoTime / tween.duration;
						break;
					case Tween.SHOCK:
						tween.orgin += Math.PI / 2;
						tween.target.y = tween.yFrom + Math.sin(tween.orgin) * 2; //Math.sin(tween.orgin) * 2; //震动强度strength为6
						break;
				}
			}
		}

		/**移除**/
		public static function remove(target:DisplayObject):void
		{
			if(containers[target] != undefined)
			{
				delete containers[target];
			}
		}

		/**
		 * 模仿TweenLite.to()方法使用。
		 * @param target
		 * @param duration
		 * @param vars Object 例如：{x:50, y:50, alpha:0.5, onComplete:myFunction, onCompleteParams:[]};
		 */
		public static function toNew(target:DisplayObject, duration:Number, vars:Object):void
		{
			var tween:Tween = (containers[target] == undefined) ? new Tween() : containers[target];
			tween.startTime = getTimer();
			tween.target = target;
			tween.duration = duration * 1000; //转换成毫秒
			tween.delay = vars.hasOwnProperty("delay") ? vars["delay"] * 1000 : 0;
			tween.type = Tween.ALL;
			tween.xFrom = target.x;
			tween.xTo = vars.hasOwnProperty("x") ? vars["x"] : target.x;
			tween.yFrom = target.y;
			tween.yTo = vars.hasOwnProperty("y") ? vars["y"] : target.y;
			tween.alphaFrom = target.alpha;
			tween.alphaTo = vars.hasOwnProperty("alpha") ? vars["alpha"] : target.alpha;
			tween.scaleX = target.scaleX;
			tween.scaleXTo = vars.hasOwnProperty("scaleX") ? vars["scaleX"] : target.scaleX;
			tween.scaleY = target.scaleY;
			tween.scaleYTo = vars.hasOwnProperty("scaleY") ? vars["scaleY"] : target.scaleY;
			tween.onComplete = vars.hasOwnProperty("onComplete") ? vars["onComplete"] : null;
			tween.onCompleteParams = vars.hasOwnProperty("onCompleteParams") ? vars["onCompleteParams"] : null;
			containers[target] = tween;
		}

		/**震动**/
		public static function shock(target:DisplayObject, duration:Number, onComplete:Function = null, onCompleteParams:Array = null):void
		{
			var tween:Tween = new Tween();
			tween.startTime = getTimer();
			tween.target = target;
			tween.duration = duration * 1000; //转换成毫秒
			tween.type = Tween.SHOCK;
			tween.orgin = 0;
			//tween.modulus = 1;
			tween.yFrom = target.y;
			tween.yTo = target.y;
			tween.xTo = target.x;
			tween.scaleXTo = target.scaleX;
			tween.scaleYTo = target.scaleY;
			tween.alphaTo = target.alpha;
			tween.onComplete = onComplete;
			tween.onCompleteParams = onCompleteParams;
			containers[target] = tween;
		}
	}
}
import flash.display.DisplayObject;

class Tween
{
	public static const ALL:int = 0;
	public static const SHOCK:int = 1;
	////////////////////////
	public var startTime:int;
	public var duration:int; //动画的缓动时间，毫秒为单位。
	public var delay:int; //延迟缓动，毫秒为单位。
	public var type:int;
	public var orgin:Number; //初始值
	public var myFinal:Number; //最终值
	public var stoped:Boolean;
	public var modulus:Number; //系数值
	public var target:DisplayObject;
	public var xFrom:Number; //初始X值
	public var xTo:Number; //结束X值
	public var yFrom:Number;
	public var yTo:Number;
	public var scaleX:Number = 1;
	public var scaleXTo:Number = 1;
	public var scaleY:Number = 1;
	public var scaleYTo:Number = 1;
	public var alphaFrom:Number = 1;
	public var alphaTo:Number = 1;
	public var onComplete:Function;
	public var onCompleteParams:Array;
}